//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////@

Log.print('service module: pektis');

Service.pektis = {};

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////@

Service.pektis.enimerosi = function(nodereq) {
	if (nodereq.isvoli()) return;
	if (nodereq.denPerastike('onoma')) return nodereq.end('Δεν περάστηκε όνομα');
	if (nodereq.denPerastike('email')) return nodereq.end('Δεν περάστηκε email');
	if (nodereq.denPerastike('kodikos')) return nodereq.end('Δεν περάστηκε κωδικός');

	setTimeout(function() {
		DB.connection().transaction(function(conn) {
			Service.pektis.enimerosi1(nodereq, conn);
		});
	}, 500);
}

Service.pektis.enimerosi1 = function(nodereq, conn) {
	var

	query = 'UPDATE `pektis` SET `onoma` = ' + globals.json(nodereq.url.onoma) +
		', `email` = ' + globals.json(nodereq.url.email);
	if (nodereq.perastike('kodikos1')) query += ', `kodikos` = ' + globals.json(nodereq.url.kodikos1);
	query += ' WHERE `login` = BINARY ' + globals.json(nodereq.loginGet()) +
		' AND `kodikos` = BINARY ' + globals.json(nodereq.url.kodikos);

	conn.queryRaw(query, function(err, res) {
		if (err || (res.affectedRows != 1)) {
			conn.rollback();
			nodereq.end('Η ενημέρωση των στοιχείων λογαριασμού απέτυχε');
			return;
		}

		conn.commit();
		nodereq.end();

		globals.initObject(nodereq.pektis, {
			onoma: nodereq.url.onoma,
			email: nodereq.url.email,
		});

		nodereq.skinikoFetch().
		kinisiAdd(new Kinisi('PK').set({
			p: new Pektis(nodereq.pektis).pektis2object().pektisIncognito(),
		}));
	});
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////@

Service.pektis.egrafi = function(nodereq) {
	if (nodereq.anonimo()) return;
	if (nodereq.denPerastike('onoma')) return nodereq.end('Δεν περάστηκε όνομα');
	if (nodereq.denPerastike('email')) return nodereq.end('Δεν περάστηκε email');
	if (nodereq.denPerastike('kodikos')) return nodereq.end('Δεν περάστηκε κωδικός');

	nodereq.url.kinisi = false;
	setTimeout(function() {
		DB.connection().transaction(function(conn) {
			Service.pektis.egrafi1(nodereq, conn);
		});
	}, 500);
}

Service.pektis.egrafi1 = function(nodereq, conn) {
	var
	query = 'INSERT INTO `pektis` (`login`, `onoma`, `email`, `kodikos`) VALUES (' +
		globals.json(nodereq.url.PK) + ', ' + globals.json(nodereq.url.onoma) + ', ' +
		globals.json(nodereq.url.email) + ', ' + globals.json(nodereq.url.kodikos) + ')';
	conn.queryRaw(query, function(err, res) {
		var pektis;

		if (err || (res.affectedRows != 1)) {
			conn.rollback();
			nodereq.end('Απέτυχε η δημιουργία λογαριασμού');
			return;
		}

		pektis = new Pektis({
			login: nodereq.url.PK,
			onoma: nodereq.url.onoma,
			email: nodereq.url.email,
		});
		nodereq.skinikoFetch().
		pektisAdd(pektis).
		kinisiAdd(new Kinisi('PK').set({
			p: new Pektis(pektis).pektis2object().pektisIncognito(),
		}), false);
		nodereq.url.kinisi = true;
		Service.sinedria.nea2ok(nodereq, conn, pektis);
	});
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////@

Service.pektis.get = function(nodereq) {
	if (nodereq.isvoli()) return;
	nodereq.end(JSON.stringify(nodereq.pektisGet()));
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////@

Service.pektis.anazitisi = function(nodereq) {
	var query, onoma, aof;

	if (nodereq.isvoli()) return;
	if (nodereq.denPerastike('onoma')) return nodereq.end('Δεν περάστηκε όνομα');

	onoma = globals.json('%' + nodereq.url.onoma + '%');
console.log('ΑΝΑΖΗΤΗΣΗ:', onoma);
	query = 'SELECT `login`, `onoma` FROM `pektis` WHERE ((`login` LIKE ' +
		onoma + ') OR (`onoma` LIKE ' + onoma + ')) ';
	query += 'ORDER BY `login` LIMIT 100';

	DB.connection().query(query, function(con, rows) {
		var skiniko = nodereq.skinikoGet(), i, login, onoma;

		con.free();

		for (i = 0; i < rows.length; i++) {
			login = rows[i]['login'];
			onoma = rows[i]['onoma'];

			sinedria = skiniko.sinedriaGet(login);
			nodereq.write('{l:' + globals.json(login) + ',o:' + globals.json(onoma) + '},');
		}
		nodereq.end();
	});
}
